﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECCIT.Data;

public partial class StatisticsShareAndDownCount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["pw"] != null && Request.QueryString["pw"] == "lattissima2012")
        {
            panel1.Visible = true;

        }
        else
        {
            panel1.Visible = false;
            return;
        }
        
    }
    protected void ShareCount_Click(object sender, EventArgs e)
    {
        ResponseInfo(true);
    }
    protected void CouponsCount_Click(object sender, EventArgs e)
    {
        ResponseInfo(false);
    }

    private  void ResponseInfo(bool flag)
    {
        var count = 0;
        if (string.IsNullOrEmpty(txt_begin.Text) || string.IsNullOrEmpty(txt_end.Text))
        {
            string sql = "select count(1) as latcount from " + (flag ? "blogXml" : "sendEmail");
            using (IDataReader reader = DBHelper.ExecuteReader(sql,new IDataParameter[]{}))                                
            {
                while (reader.Read())
                {
                    count = Convert.ToInt32(reader["latcount"]);
                }
            }
        }
        else
        {
            DateTime begintime, endtime;
            if (!DateTime.TryParse(txt_begin.Text, out begintime))
            {
                Alert("请输入正确的时间！");
                return;
            }
            if (!DateTime.TryParse(txt_end.Text, out endtime))
            {
                Alert("请输入正确的时间！");
                return;
            }

            string sql = "select count(1) as latcount from " + (flag ? "blogXml" : "sendEmail") + " where creatTIme between @begintime and @endtime";
            using (IDataReader reader = DBHelper.ExecuteReader(sql,
                                   new IDataParameter[]
                                   {
                                       //new SqlParameter("tablename", SqlDbType.NChar)
                                       //    {Value = flag ? "blogXml" : "sendEmail"},
                                       new SqlParameter("begintime", SqlDbType.DateTime) {Value = begintime},
                                       new SqlParameter("endtime", SqlDbType.DateTime) {Value = endtime}
                                   }))
            {
                while (reader.Read())
                {
                    count = Convert.ToInt32(reader["latcount"]);
                }
            }
            
        }
        
        ltl_count.Text = (flag ? "Share" : "Coupons") + "Count:" + count;
    }

    public static void Alert(string str)
    {
        string s = "alert(\"" + str + "\");";
        HttpContext.Current.Response.Write("<script language=\"javascript\">" + s + ";history.go(-1)</script>");
    }
}